/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Agency.models;
import Agency.views.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;

/**
 *
 * @author wangari
 */

public class SearchTenantModel extends javax.swing.JFrame {
private ActionListener actionlistener;
public  SearchTenantView mySearch;
Connection conn = null;

/**
 * a constructor that initializes an object of the search view 
 */
public SearchTenantModel(){
  mySearch= new   SearchTenantView();
 //mySearch.setVisible(true);
}
/**
 * method to close the view, setting it to false
 */
public void closePage(){
  mySearch.setVisible(false);   
}
/**
 * A method that connects to the database and search for a tenant  and displays  feeds the 
 * tenants information to the textField and  the combo box. it uses the tenants id number to search since its a unique key.
 * two people cannot share the same identification number
 */

public void searchTenant(){
    String    theTenantsIdNo = mySearch.searchId().getText();
         try {
      //Connecting to the database      
       Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/MusyiAgency?user=root&password=");
 System.out.println("connected to database");
        PreparedStatement p =conn.prepareStatement("SELECT * FROM TenantsInformation WHERE TenantsIdNo = "+theTenantsIdNo+";");

// result set from the database
  java.sql.ResultSet r = p.executeQuery ();
  while(r.next()) {
      mySearch.getTheName().setText(r.getString("TenantsName"));
      mySearch.getIdNum().setText(r.getString("TenantsIdNo"));
      mySearch.getPhoneNum().setText(r.getString("TenantsPhoneNumber"));
      mySearch.getHouseType().setSelectedItem(r.getString("HouseType"));
      mySearch.getPlotNumber().setText(r.getString("HousePlotNum"));
     mySearch.getTownLocation().setSelectedItem(r.getString("Town"));
     mySearch.getGender().setSelectedItem(r.getString("Gender"));
      mySearch.getOccupation().setText(r.getString("Occupation"));
      
  }
}
       catch (Exception e) {
  JOptionPane.showMessageDialog(null,e);
        }
         }

}

